import 'dart:async';

import 'package:aegis_flutter_sdk/aegis_flutter_sdk.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'infinite_list/catalog.dart';
import 'infinite_list/infinite_list_page.dart';
import 'util/aegis_manager.dart';

void main() {
  runZonedGuarded(() async {
    WidgetsFlutterBinding.ensureInitialized();
    // 先初始化 Aegis 组件，因为需要计算页面渲染时间
    AegisManager();

    runApp(MyApp());
  }, (Object error, StackTrace stack) async {
    AegisManager()
        .ageis
        .error('flutter uncaught error, error:$error, stackTrace:$stack');
  });
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Demo(),
      navigatorObservers: [PageNavigatorObserver()],
    );
  }
}

class Demo extends StatelessWidget {
  // 项目id需要在https://tam.woa.com/role/group-projects-manage内申请
  final aegis = AegisManager().ageis;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo'),
      ),
      body: ListView(
        children: [
          TextButton(
            onPressed: onPressedInfoAll,
            child: Text('aegis.infoAll [日志上报]'),
            style: TextButton.styleFrom(
              primary: Colors.green,
            ),
          ),
          TextButton(
            onPressed: onPressedInfo,
            child: Text('aegis.info [白名单日志上报]'),
            style: TextButton.styleFrom(
              primary: Colors.blue,
            ),
          ),
          TextButton(
            onPressed: onPressedReport,
            child: Text('aegis.report [自定义错误日志上报]'),
            style: TextButton.styleFrom(
              primary: Colors.orange,
            ),
          ),
          TextButton(
            onPressed: onPressedError,
            child: Text('aegis.error [系统级error日志上报]'),
            style: TextButton.styleFrom(
              primary: Colors.red,
            ),
          ),
          TextButton(
            onPressed: onPressedEvent,
            child: Text('aegis.event'),
            style: TextButton.styleFrom(
              primary: Colors.red,
            ),
          ),
          TextButton(
            onPressed: onPressedReporttime,
            child: Text('aegis.reportTime'),
            style: TextButton.styleFrom(
              primary: Colors.teal,
            ),
          ),
          TextButton(
            onPressed: onPressedTime,
            child: Text('aegis.time'),
            style: TextButton.styleFrom(
              primary: Colors.lightGreen,
            ),
          ),
          TextButton(
            onPressed: onPressedTimeEnd,
            child: Text('aegis.timeEnd'),
            style: TextButton.styleFrom(
              primary: Colors.lightGreen,
            ),
          ),
          TextButton(
            onPressed: () {
              openInfiniteListPage(context);
            },
            child: Text('打开无限List流测试上报页面渲染时间'),
            style: TextButton.styleFrom(
              primary: Colors.deepPurple,
            ),
          ),
        ],
      ),
    );
  }

  void onPressedInfo() {
    aegis.info({
      'text': 'test',
      'number': 1,
      'cool': {'a': 3}
    });
  }

  void onPressedReport() {
    aegis.report('report');
  }

  void onPressedInfoAll() {
    aegis.infoAll({
      'text': 'inforest',
      'number': 1,
      'cool': {'a': 3}
    });
  }

  void onPressedError() {
    aegis.error('error');
  }

  void onPressedEvent() {
    aegis.reportEvent('example event 2', ext1: 'testExt1');
  }

  void onPressedReporttime() {
    aegis.reportTime('exampletime2', 1000);
  }

  void onPressedTime() {
    aegis.time('customTime2');
  }

  void onPressedTimeEnd() {
    aegis.timeEnd('customTime2');
  }

  void openInfiniteListPage(BuildContext context) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (BuildContext context) {
          return ChangeNotifierProvider<Catalog>(
            create: (context) => Catalog(),
            child: InfiniteListPage(),
          );
        },
        settings: RouteSettings(name: 'InfiniteListPage'),
      ),
    );
  }
}
